/**
 * 
 */
package com.sx.feesunit.action;

import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.opensymphony.xwork2.ModelDriven;
import com.sx.core.BaseAction;
import com.sx.core.JSONUtils;
import com.sx.feesunit.biz.IAreaBiz;
import com.sx.feesunit.entity.AreaEntity;
import com.sx.feesunit.exception.SXException;

/**
 * @author Administrator
 *
 */
@SuppressWarnings("serial")
public class AreaAction extends BaseAction implements ModelDriven<AreaEntity>{
	private IAreaBiz iareabiz;
	private AreaEntity area = new AreaEntity();
	private Log log = LogFactory.getLog(this.getClass());
	/**
	 * 获取小区信息Tree列表数据信息 
	 **/
	public void findAllAreaForTree() throws Exception{
		try {
			List<AreaEntity> list = this.iareabiz.findAreaInfo(area);
			String str = JSONArray.toJSONString(list);
			str = str.replaceAll("name","text");
			super.outJsonString(str);
		} catch (Exception e) {
			log.error("查询小区失败出错",e);
			super.outJsonString("{success:false,data:{}}");
			throw new SXException(e);
		}
	}
	
	/**
	 *查找全部小区信息 
	 **/
	public void findAllAreaInfo()throws Exception{
		try{
			List<AreaEntity> list = this.iareabiz.findAreaInfo(area);
			String str = JSONArray.toJSONString(list);
			super.outJsonString(str);
		}catch(Exception e){
			log.error("查询小区失败出错",e);
			super.outJsonString("{success:false,data:{}}");
			throw new SXException(e);
		}
	}
	public void addAreaInfo() throws SXException{
		try {
			Integer id = iareabiz.addAreaInfo(area);
			if (id.equals(0)) {
				outJsonString("{success:false,aid:0}");
			} else {
				outJsonString("{success:true,aid:"+id.intValue()+"}");
			}
		} catch (Exception e) {
			log.error("新增小区出错",e);
			super.outJsonString("{success:false,aid:0}");
			throw new SXException(e);
			// TODO: handle exception
		}
	}
	public void updateAreaInfo() throws SXException{
		try {
			boolean flag = iareabiz.updateAreaInfo(area);
			if (flag) {
				outJsonString("{success:true}");
			} else {
				outJsonString("{success:false}");
			}
		} catch (Exception e) {
			log.error("修改小区出错",e);
			super.outJsonString("{success:false}");
			throw new SXException(e);
			// TODO: handle exception
		}
	}
	public void deleteAreaInfo() throws SXException{
		try {
			boolean flag = iareabiz.deleteAreaInfo(area);
			if (flag) {
				outJsonString("{success:true}");
			} else {
				outJsonString("{success:false}");
			}
		} catch (Exception e) {
			log.error("删除小区出错",e);
			super.outJsonString("{success:false}");
			throw new SXException(e);
			// TODO: handle exception
		}
	}
	/**
	 *单个查询 
	 **/
	public void findAreaInfo()throws Exception{
		try{
			List<AreaEntity> list = this.iareabiz.findAreaInfo(area);
			if(list.size()!=0){
				AreaEntity areainfo = list.get(0);
				String str = JSONObject.toJSONString(areainfo);
				super.outJsonString("{success:true,data:"+str+"}");
			}else{
				super.outJsonString("{success:false,data:{}}");
			}
		}catch(Exception e){
			log.error("查询小区失败出错",e);
			super.outJsonString("{success:false,data:{}}");
			throw new SXException(e);
		}
	}
	/**
	 *And连接分页查询 
	 **/
	public void findAreaInfoByAndCondiction() throws Exception{
		String str = "{totalCount:0,data:[]}";
		try{
			List<AreaEntity> list = this.iareabiz.findAreaInfoByAndCondiction(area);
			if(list.size()!=0){
				str = JSONUtils.convertListToJson(AreaEntity.class, list, list.get(0).getTotal());
			}
			outJsonString(str);
		}catch(Exception e){
			log.error("小区查询And失败出错",e);
			outJsonString(str);
			throw new SXException(e);
		}
	}
	/**
	 *Or连接分页查询 
	 **/
	public void findAreaInfoByOrCondiction() throws Exception{
		String str = "{totalCount:0,data:[]}";
		try{
			List<AreaEntity> list = this.iareabiz.findAreaInfoByOrCondiction(area);
			if(list.size()!=0){
				str = JSONUtils.convertListToJson(AreaEntity.class, list, list.get(0).getTotal());
			}
			outJsonString(str);
		}catch(Exception e){
			log.error("小区查询Or失败出错",e);
			outJsonString(str);
			throw new SXException(e);
		}
	}
	/**
	 * @return the iareabiz
	 */
	public IAreaBiz getIareabiz() {
		return iareabiz;
	}

	/**
	 * @param iareabiz the iareabiz to set
	 */
	public void setIareabiz(IAreaBiz iareabiz) {
		this.iareabiz = iareabiz;
	}
	public AreaEntity getModel() {
		// TODO Auto-generated method stub
		return area;
	}
	
}
